/*
 * @(#)UserInterceptor.java 2013-4-10 ����02:26:57 Order
 */
package com.dingtalk.dingbotmgr.interceptor;

import java.util.Map;

import javax.annotation.Resource;

import com.dingtalk.dingbotmgr.action.HomeAction;
import com.dingtalk.dingbotmgr.action.LoginUserAwareBaseAction;
import com.dingtalk.dingbotmgr.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import org.springframework.stereotype.Component;

/**
 * UserInterceptor
 *
 * @author wang
 * @version 1.0
 */
@Component("userInterceptor")
public class UserInterceptor implements Interceptor {

    public static final String USER_SESSION_KEY = "_user_";

    @Resource(name = "userService")
    private UserService userService;

    @Override
    public void destroy() {

    }

    @Override
    public void init() {

    }

    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        Map<String, Object> session = ai.getInvocationContext().getSession();
        Integer id = (Integer)session.get(UserInterceptor.USER_SESSION_KEY);
        if (ai.getAction() instanceof LoginUserAwareBaseAction) {
            LoginUserAwareBaseAction action = (LoginUserAwareBaseAction)ai.getAction();
            if (id != null) {
                action.setLoginUser(userService.getEntityById(id));
            } else {
                if (!(action instanceof HomeAction)) {
                    return Action.LOGIN;
                }
                //            id = 1;
                //            action.setLoginUser(userService.getEntityById(id));
            }
        }
        String ret = ai.invoke();
        if (ai.getAction() instanceof LoginUserAwareBaseAction) {
            LoginUserAwareBaseAction action = (LoginUserAwareBaseAction)ai.getAction();
            if (id == null && action.getLoginUser() != null) {
                session.put(USER_SESSION_KEY, action.getLoginUser().getId());
            }
            if (id != null && action.getLoginUser() == null) {
                session.remove(USER_SESSION_KEY);
            }
        }
        return ret;
    }
}
